from urllib import request, parse
import json

def translate(keyword):
    base_url = 'https://fanyi.baidu.com/sug'
    data = {
        'kw': keyword
    }
    data = parse.urlencode(data)
    header = {"User-Agent": "mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"}
    req = request.Request(url=base_url, data=bytes(data, encoding='utf-8'), headers=header)
    res = request.urlopen(req)
    str_json = res.read().decode('utf-8')
    myjson = json.loads(str_json)
    # print(myjson)

    info = myjson['data'][0]['v']
    print(info)

if __name__ == '__main__':
    while True:
        keyword = input("请输入要翻译的单词（输入!q退出）：\n")
        if keyword == '!q':
            break
        translate(keyword)